package com.eascs.ticket.project.controller;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.common.util.DateUtils;
import com.eascs.common.util.ObjectUtil;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.MessageRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.common.controller.BaseActionRestController;
import com.eascs.ticket.project.entity.ProjectVersion;
import com.eascs.ticket.project.entity.ProjectVersionUpdate;
import com.eascs.ticket.project.form.GetInsertProjectVersionPageForm;
import com.eascs.ticket.project.form.GetUpdateProjectVersionPageForm;
import com.eascs.ticket.project.form.ProjectVersionDeleteForm;
import com.eascs.ticket.project.form.ProjectVersionInsertForm;
import com.eascs.ticket.project.form.ProjectVersionUpdateForm;
import com.eascs.ticket.project.service.ProjectVersionService;

@RestController
public class ProjectVersionRestController extends BaseActionRestController {
	@Inject
	private ProjectVersionService projectVersionService;

	@RequestMapping(value = "/project/ticket-version/getInsertProjectVersionPage")
	public HtmlRestResult getInsertProjectVersionPage(GetInsertProjectVersionPageForm getInsertProjectVersionPageForm) {
		ViewContext.put("projectId", getInsertProjectVersionPageForm.getProjectId());
		return HtmlRestResult.popHtmlFragment("project/getInsertProjectVersionPage");
	}

	@RequestMapping(value = "/project/ticket-version/getUpdateProjectVersionPage")
	public HtmlRestResult getUpdateProjectVersionPage(GetUpdateProjectVersionPageForm getUpdateProjectVersionPageForm) {
		ViewContext.put("projectVersion", this.projectVersionService.getProjectVersionById(getUpdateProjectVersionPageForm.getId()));
		return HtmlRestResult.popHtmlFragment("project/getUpdateProjectVersionPage");
	}

	@RequestMapping(value = "/project/ticket-version/insert")
	public MessageRestResult insertProjectVersion(ProjectVersionInsertForm projectVersionInsertForm) {
		ProjectVersion projectVersion = new ProjectVersion();
		ObjectUtil.copySimpleProperties(projectVersionInsertForm, projectVersion);
		projectVersion.setEndTime(new java.sql.Date(DateUtils.addDate(projectVersionInsertForm.getEndTime(), 1).getTime() - 1));
		projectVersionService.insertProjectVersion(projectVersion);
		return MessageRestResult.refresh();
	}

	@RequestMapping(value = "/project/ticket-version/update")
	public MessageRestResult updateProjectVersion(ProjectVersionUpdateForm projectVersionUpdateForm) {
		ProjectVersionUpdate projectVersionUpdate = new ProjectVersionUpdate();
		ObjectUtil.copySimpleProperties(projectVersionUpdateForm, projectVersionUpdate);
		projectVersionUpdate.setEndTime(new java.sql.Date(DateUtils.addDate(projectVersionUpdateForm.getEndTime(), 1).getTime() - 1));
		projectVersionService.updateProjectVersion(projectVersionUpdate);
		return MessageRestResult.refresh();
	}

	@RequestMapping(value = "/project/ticket-version/delete")
	public MessageRestResult deleteProjectVersion(ProjectVersionDeleteForm projectVersionDeleteForm) {
		this.projectVersionService.deleteProjectVersion(projectVersionDeleteForm.getId());
		return MessageRestResult.refresh();
	}

}
